import Vue from "vue";
import Router from "vue-router";
import Login from "@/pages/login";

Vue.use(Router);

function route (path, file, name,title,redirect,children) {
  return{
    exact: true,
    path,
    name,
    children:children,
    redirect:redirect,
    meta:{title},
    component: () => import('../pages' + file)
  }
}
function option() {
  let optionOneRouter = route("optionOne","/navigationOne/optionOne","optionOne","待办事项");
  let optionThreeRouter = route("optionThree","/navigationOne/optionThree","optionThree","已办事项");
  let homePage = route("option","/navigationOne/option","option","个人办公","/option/optionOne",[optionOneRouter,optionThreeRouter]);
  return homePage;
}
function refresh() {
  let refresh = route("refresh","/refresh","refresh");
  return refresh;
}

function systemRouter() {
  let dictionary = route("dictionary","/system/dictionary","dictionary","字典管理");
  let menu = route("systemMenu","/system/systemMenu","systemMenu","菜单管理");
  let system = route("system","/system","system","系统管理","/system/dictionary",[dictionary,menu]);

  return system;
}
function departmentRouter() {
  let department = route("department","/administration/department","department","部门管理");
  let user = route("user","/administration/user","user","人员管理");
  let administration = route("administration","/administration","administration","行政管理","/administration/department",[department,user]);
  return administration;
}
function configurationRouter() {
  let permission = route("permission","/configuration/permission","permission","权限管理");
  let fileManage = route("fileManage","/configuration/fileManage","fileManage","权限管理");
  let configuration = route("configuration","/configuration","configuration","业务配置","/configuration/permission",[permission,fileManage]);
  return configuration;
}

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: "/login"
    },
    route("/login","/login","Login"),
    {
      path:"/", // 根路径，路由到 Layout组件
      redirect:"option",
      component: () => import('../pages/layout/layout'),
      children:[ // 其它所有组件都是 ElementDemo的子组件
        refresh(),
        option(),
        systemRouter(),
        departmentRouter(),
        configurationRouter()

      ]
    },
    route("/test1","/test/test1","test1"),
    // {
    //   path: "*",
    //   redirect: "/login"
    // }
  ]
});

//解决重复点击路由时，控制台报错
//获取原型对象上的push函数
const originalPush = Router.prototype.push;
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

// 全局前置守卫
// 当一个导航触发时，全局前置守卫按照创建顺序调用。
// 守卫是异步解析执行，此时导航在所有守卫 resolve 完之前一直处于 等待中
router.beforeEach((to, from, next) => {
  // 想办法获取token，一般都是存储在vuex中
  let empId = window.localStorage.getItem("empId");
  // 存在token就让他登录，跳转到登录页写好的跳转地址
  if (empId) {
    next();
  } else {
    // 否则就让他跳转到登录页
    if (to.path === "/login") {
      next();
    } else {
      alert("没有访问权限或登录已过期，请重新登录！");
      next("/login");
    }
  }
});

export default router;
